<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="学期名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="semesterName">
              <a-input v-model="model.semesterName" :maxLength="30" placeholder="请输入学期名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="学期日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="date">
              <a-range-picker @change="onChange" valueFormat="YYYY-MM-DD"  v-model="model.date" style="width:100%;"/>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="学期开始日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startDate">
              <!-- <j-date placeholder="请选择开始日期" v-model="model.startDate"   /> -->
              <a-date-picker
                v-model="model.startDate"
                :disabledDate="disabledStartDate"
                format="YYYY-MM-DD"
                style="width: 100%"
                placeholder="请选择开始日期"
                @change="handleStartDateChange"
                @openChange="handleStartOpenChange"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="学期结束日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endDate">
              <!-- <j-date placeholder="请选择结束日期" v-model="model.endDate"  style="width: 100%" /> -->
              <a-date-picker
                v-model="model.endDate"
                :disabledDate="disabledEndDate"
                format="YYYY-MM-DD"
                placeholder="请选择结束日期"
                style="width: 100%"
                :open="endOpen"
                @change="handleEndDateChange"
                @openChange="handleEndOpenChange"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import moment from 'moment';
  export default {
    name: 'SkSemesterForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          semesterName:[
            { required: true, message: '请输入学期名称!'},
          ],
          // date:[
          //   { required: true, message: '请选择学期日期!' ,trigger:"change"},
          // ],
          startDate: [{ required: true, message: '请输入开始日期！' }],
           endDate: [{ required: true, message: '请输入结束日期！' }],
        },
        url: {
          add: "/management/skSemester/add",
          edit: "/management/skSemester/edit",
          queryById: "/management/skSemester/queryById"
        },
        endOpen: false,
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    mounted(){
    },
    methods: {
      disabledStartDate(startValue) {
        const endValue = this.model.endDate;
        if (!startValue || !endValue) {
          return false;
        }
        return startValue.valueOf() > endValue.valueOf();
      },
      disabledEndDate(endValue) {
        const startValue = this.model.startDate;
        if (!endValue || !startValue) {
          return false;
        }
        return startValue.valueOf() >= endValue.valueOf();
      },
      // 结束日期Change事件
      handleStartDateChange(val, dateStr) {
        this.model.startDate = dateStr
      },
      // 结束日期Change事件
      handleEndDateChange(val, dateStr) {
        this.model.endDate = dateStr
      },
      handleStartOpenChange(open) {
        if (!open) {
          this.endOpen = true;
        }
      },
      handleEndOpenChange(open) {
        this.endOpen = open;
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        // if (this.model.id) {
        //   this.model.startDate = moment(record.startDate);
        //   this.model.endDate = moment(record.endDate);
        // }
        this.visible = true;
      },
      onChange(date,dateString){
        this.model.date = [moment(dateString[0]), moment(dateString[1])];
        this.model.startDate = dateString[0]
        this.model.endDate = dateString[1]
        this.$forceUpdate();
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>